// 封装分类数据业务相关代码
import { getCategoryAPI } from '@/apis/category';
import { onMounted, ref } from 'vue';
import { useRoute, onBeforeRouteUpdate } from 'vue-router';

export function useCategory() {
  const route = useRoute();
  const category = ref({});
  const getCategory = async (id = route.params.id) => {
    category.value = (await getCategoryAPI(id)).result;
  };

  onMounted(() => getCategory()); 

  // 在路由变化时获取新路由对应的数据
  onBeforeRouteUpdate((to) => getCategory(to.params.id));
  
  return {
    category
  };
};